﻿#include <QFile>
#include <QBitmap>
#include <QMessageBox>
#include <QStyleFactory>
#include <QMenu>
#include "CPublicData.h"
#include "CRobWayPlanDialog.h"
#include <QTimer>
#include "CSqlOper.h"
//#include "CRobSceneFactNd.h"
#include "CRBaseData.h"
#include "CHeader.h"
#include "CRobPointConfigDialog.h"
#include "CRobMovePointDlg.h"

CRobWayPlanDialog::CRobWayPlanDialog(QString sSceneGuid, CDevBase* pDev, CRBaseData* pBasicData, QWidget *parent /*= nullptr*/)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	m_sCopyItemGuid = "";
	m_sCopyPointGuid = "";
	m_sGuid = sSceneGuid;
	m_pDev = pDev;
	m_pBasicData = pBasicData;
	m_pDevData = (CDevDataBase*)pDev->m_pDevDataBase;
	m_nSpeed = m_pDevData->m_dRobRunSpeedSet.toFloat()*10;
	m_nIsMove = 0;
	m_bIsStopButtonClicked = false;
	m_pPolAreaOld = new CPolArea();
	m_pBasicData->m_pMutex->lock();
	m_pBasicData->m_pPolArea->copyData(m_pPolAreaOld);
	m_pBasicData->m_pMutex->unlock();
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &CRobWayPlanDialog::slotRefushData);
	m_pTimer->start(500);
	setStyleSheetFile();
	initUI();
}

CRobWayPlanDialog::~CRobWayPlanDialog()
{
}

void CRobWayPlanDialog::closeEvent(QCloseEvent * event)//关闭对话时
{
	//遍历是否存在未保存的巡检点
	bool bFind = false;
	for (QMap<QString, CPolPoint*>::iterator itor = m_pPolAreaOld->m_mapPolPoint.begin(); itor != m_pPolAreaOld->m_mapPolPoint.end(); itor++)
	{
		CPolPoint* pPoint = itor.value();
		if (pPoint->m_nPointStatus != 0)
		{
			bFind = true;
			break;//只要找到一个没保存的就直接播报存在未保存的
		}
		bool bItemFind = false;
		for (QMap<QString, CPolItem*>::iterator it = pPoint->m_mapPolItem.begin(); it != pPoint->m_mapPolItem.end(); it++)
		{
			CPolItem* pItem = it.value();
			if (pItem->m_nItemStatus != 0)
			{
				bItemFind = true;
				break;
			}
		}
		if (bItemFind == true)
		{
			bFind = true;
			break;
		}
	}
	if (bFind == true)
	{
		int nRet = messageBox(3, A2T("存在未保存的记录，是否需要保存后再关闭此对话框?"));
		if (nRet == QMessageBox::Yes)
		{
			slotPushButtonSaveClicked();
			event->accept();
		}
		else if (nRet == QMessageBox::No)
		{
			event->accept();
		}
		else if (nRet == QMessageBox::Close)
		{
			event->ignore();
		}
	}
}

void CRobWayPlanDialog::setVideoData()
{
	ui.widgetVideo->m_pDev = m_pDev;
	ui.widgetVideo->m_sGuid = m_sGuid;
	ui.widgetVideo->playVideo();
}

void CRobWayPlanDialog::setStyleSheetFile()
{
	////设置对话框标题
	//this->setWindowTitle(A2T("模版管理"));
	//QString sQss = setUiStyle("page");
	//QString sMyqss = styleSheet();
	//QString sMerged = sQss + "\n" + sMyqss; // 后面的会覆盖前面的相同规则	
	//setStyleSheet(sMerged);
}

void CRobWayPlanDialog::initConnect()
{
	connect(ui.treeView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(slotCustomContextMenu(const QPoint &)));//连接点击右键信号与槽函数
	connect(ui.widgetMap, SIGNAL(sigAddPoint(QPointF,int)), this, SLOT(slotAddPoint(QPointF,int)));//添加巡检点
	connect(ui.widgetMap, SIGNAL(sigEditPoint(int)), this, SLOT(slotEditPoint(int)));              //编辑巡检点
	connect(ui.widgetMap, SIGNAL(sigMovePoint(int)), this, SLOT(slotMovePoint(int)));              //编辑巡检点
	connect(ui.widgetMap, SIGNAL(sigDelPoint(int)), this, SLOT(slotDelPoint(int)));                //删除巡检点
	connect(ui.widgetMap, SIGNAL(sigSelectedPoint(int)), this, SLOT(slotSelectedPoint(int)));      //选中巡检点
	connect(ui.widgetMap, SIGNAL(sigMoveMent(int)), this, SLOT(slotMoveMent(int)));      //选中巡检点
	
	connect(ui.pushButtonEmergencyStop, SIGNAL(clicked()), this, SLOT(slotPushButtonEmergencyStopClicked()));
	connect(ui.pushButtonAdd, SIGNAL(clicked()), this, SLOT(slotPushButtonAddClicked()));
	connect(ui.pushButtonChange, SIGNAL(clicked()), this, SLOT(slotPushButtonChangeClicked()));
	connect(ui.pushButtonDelete, SIGNAL(clicked()), this, SLOT(slotPushButtonDeleteClicked()));
	connect(ui.pushButtonReChange, SIGNAL(clicked()), this, SLOT(slotPushButtonReChangeClicked()));
	connect(ui.pushButtonSave, SIGNAL(clicked()), this, SLOT(slotPushButtonSaveClicked()));
	connect(ui.pushButtonExpand, SIGNAL(clicked()), this, SLOT(slotExpand()));
	connect(ui.pushButtonShrink, SIGNAL(clicked()), this, SLOT(slotShrink()));

	connect(ui.widgetControlButton->ui.pushButtonDeSpeed, &QPushButton::clicked, this, &CRobWayPlanDialog::slotPushButtonDeSpeedClicked);
	connect(ui.widgetControlButton->ui.pushButtonAddSpeed, &QPushButton::clicked, this, &CRobWayPlanDialog::slotPushButtonAddSpeedClicked);
	connect(ui.widgetControlButton->ui.pushButtonRobResetLoc, &QPushButton::clicked, this, &CRobWayPlanDialog::slotPushButtonResetLocClicked);
	connect(ui.widgetControlButton->ui.pushButtonTalkBackFunc, &QPushButton::clicked, this, &CRobWayPlanDialog::slotPushButtonTalkBackFuncClicked);
	connect(ui.widgetControlButton->ui.pushButtonTalkBackClose, &QPushButton::clicked, this, &CRobWayPlanDialog::slotPushButtonTalkBackCloseClicked);
	connect(ui.widgetControlButton->ui.pushButtonLampLightOpen, &QPushButton::clicked, this, &CRobWayPlanDialog::slotPushButtonLampLightOpenClicked);
	connect(ui.widgetControlButton->ui.pushButtonLampLightClose, &QPushButton::clicked, this, &CRobWayPlanDialog::slotPushButtonLampLightCloseClicked);
	connect(ui.widgetControlButton->ui.pushButtonFocusingAdd, &QPushButton::pressed, this, &CRobWayPlanDialog::slotPushButtonFocusingAddClicked);
	connect(ui.widgetControlButton->ui.pushButtonFocusingAdd, &QPushButton::released, this, &CRobWayPlanDialog::slotPushButtonFocusingAddRelease);
	connect(ui.widgetControlButton->ui.pushButtonFocusingSubtract, &QPushButton::pressed, this, &CRobWayPlanDialog::slotPushButtonFocusingSubtractClicked);
	connect(ui.widgetControlButton->ui.pushButtonFocusingSubtract, &QPushButton::released, this, &CRobWayPlanDialog::slotPushButtonFocusingSubtractRelease);

	//CPushButtonA中的槽函数绑定
	//机器人控制部分
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(PressedUp()), this, SLOT(slotRobRunUp()));       //上
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(PressedDown()), this, SLOT(slotRobRunDown()));   //下
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(PressedLeft()), this, SLOT(slotRobRunLeft()));  //前进
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(PressedRigth()), this, SLOT(slotRobRunRight()));  //后退
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(ReleasedUp()), this, SLOT(slotRobRunStop()));    //停止运动
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(ReleasedDown()), this, SLOT(slotRobRunStop()));  //停止运动
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(ReleasedLeft()), this, SLOT(slotRobRunStop()));  //停止运动
	connect(ui.widgetControlButton->ui.pushButtonControl, SIGNAL(ReleasedRigth()), this, SLOT(slotRobRunStop())); //停止运动

	//云台控制部分
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(PressedUp()), this, SLOT(slotYtUpP()));               //云台 按下 上
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(PressedDown()), this, SLOT(slotYtDownP()));           //云台 按下 下
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(PressedLeft()), this, SLOT(slotYtLeftP()));           //云台 按下 左
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(PressedRigth()), this, SLOT(slotYtRigthP()));         //云台 按下 右
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(ReleasedUp()), this, SLOT(slotYtUpR()));              //云台 松开 上
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(ReleasedDown()), this, SLOT(slotYtDownR()));          //云台 松开 下
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(ReleasedLeft()), this, SLOT(slotYtLeftR()));          //云台 松开 左
	connect(ui.widgetControlButton->ui.PushButtenYt, SIGNAL(ReleasedRigth()), this, SLOT(slotYtRigthR()));        //云台 松开 右

}

void CRobWayPlanDialog::initUI()
{
	QIcon icon(":/icon/Rob/Rob.ico");
	setWindowIcon(icon);
	setWindowTitle(A2T("巡检路径规划"));

	ui.widgetControlButton->ui.pushButtonControl->SetPushButtenText("", A2T("上升"), A2T("下降"), A2T("后退"), A2T("前进"));
	ui.widgetControlButton->ui.PushButtenYt->SetPushButtenText("", A2T("上"), A2T("下"), A2T("左"), A2T("右"));

	m_pTreeModel = new QStandardItemModel();
	ui.treeView->setModel(m_pTreeModel);
	ui.treeView->setStyle(QStyleFactory::create("windows"));
	ui.treeView->setSelectionMode(QAbstractItemView::SingleSelection);
	ui.treeView->setFrameShape(QFrame::NoFrame);
	ui.treeView->header()->hide();
	ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu);
	ui.treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
	ui.treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	ui.treeView->setFocusPolicy(Qt::NoFocus);

	initConnect();
	setEmergencyStopUp();
	refushPointList();
	refushMap();
	setVideoData();
}

void CRobWayPlanDialog::setEmergencyStopDown()
{
	QPixmap pix(":/icon/Rob/press.png");
	ui.pushButtonEmergencyStop->setStyleSheet("border-image: url(:/icon/Rob/press.png);");
	ui.pushButtonEmergencyStop->setMask(pix.mask());
}

void CRobWayPlanDialog::setEmergencyStopUp()
{
	QPixmap pix(":/icon/Rob/notPress.png");
	ui.pushButtonEmergencyStop->setStyleSheet("border-image: url(:/icon/Rob/notPress.png);");
	ui.pushButtonEmergencyStop->setMask(pix.mask());
}

bool CRobWayPlanDialog::setChargePoint(QString sPointGuid, int nSetValue)
{
	if (m_pPolAreaOld->m_mapPolPoint.contains(sPointGuid))
	{
		CPolPoint* pPolPoint = m_pPolAreaOld->m_mapPolPoint[sPointGuid];
		pPolPoint->m_nIsChargePoint = nSetValue;
		if (pPolPoint->m_nPointStatus == 0)//如果这个巡检点不是新增也没有修改过，则把他设置成已经修改过
		{
			pPolPoint->m_nPointStatus = 2;
		}
		else if (pPolPoint->m_nPointStatus == 1)//如果这个巡检点是新增的，则他的状态还是新增
		{
			pPolPoint->m_nPointStatus = 1;
		}
		else if (pPolPoint->m_nPointStatus == 2)//如果这个巡检项之间修改过，则他的状态还是修改过
		{
			pPolPoint->m_nPointStatus = 2;
		}
		return true;
	}
	else
	{
		return false;
	}
}

void CRobWayPlanDialog::pasteItem(QString sPastePointGuid, QString sCopyPointGuid, QString sCopyItemGuid)
{
	if (sPastePointGuid == "")
	{
		return;
	}
	if (sCopyItemGuid == ""|| sCopyPointGuid=="")
	{
		messageBox(0, A2T("请先复制巡检项"));
		return;
	}
	CPolPoint* pCopyPoint = m_pPolAreaOld->m_mapPolPoint[sCopyPointGuid];//被复制的巡检点
	CPolItem* pCopyItem;//被复制的巡检项
	for (QMap<QString, CPolItem*>::iterator itor = pCopyPoint->m_mapPolItem.begin(); itor != pCopyPoint->m_mapPolItem.end(); itor++)//首先找到被复制的巡检项指针
	{
		CPolItem* pItem = itor.value();
		if (m_sCopyItemGuid == pItem->m_sItemGuid)//找到了需要复制的巡检项
		{
			pCopyItem = pItem;
			break;
		}
		else
		{
			continue;
		}
	}

	//找到粘贴的巡检项
	CPolPoint* pPastePoint = m_pPolAreaOld->m_mapPolPoint[sPastePointGuid];//被粘贴的巡检点
	CPolItem* pNewItem;
	if (pCopyItem->m_nDevType == 0)//传感器类型
	{
		pNewItem = new CPolItemSensor();
	}
	else if (pCopyItem->m_nDevType == 1)//云台类型
	{
		pNewItem = new CPolItemCamer();
	}
	QString sGuid = createGuid();
	pCopyItem->copyData(pNewItem);
	pNewItem->m_nIndex= m_pPolAreaOld->m_mapPolPoint[sCopyPointGuid]->getItemMaxIndex() + 1;
	pNewItem->m_sItemGuid = sGuid;
	pNewItem->m_nItemStatus = 1;//新增巡检项
	pNewItem->m_sPointGuid = sPastePointGuid;
	pPastePoint->m_mapPolItem.insert(sGuid, pNewItem);
	if (pPastePoint->m_nPointStatus == 0)
	{
		pPastePoint->m_nPointStatus = 2;
	}
	else if (pPastePoint->m_nPointStatus == 1)
	{
		pPastePoint->m_nPointStatus = 1;
	}
	else if (pPastePoint->m_nPointStatus == 2)
	{
		pPastePoint->m_nPointStatus = 2;
	}
	m_sCopyItemGuid = "";
	m_sCopyPointGuid = "";
	refushPointList();
}

void CRobWayPlanDialog::refushPointList()
{
	if (m_pTreeModel != nullptr)
	{
		for (int i = 0; i < m_pTreeModel->rowCount(); i++)
		{
			QStandardItem *pItem = m_pTreeModel->item(i);

			for (int j = 0; j < pItem->rowCount(); j++)
			{
				QStandardItem *SubpItem = pItem->child(i);
				delete SubpItem;
				SubpItem = nullptr;
			}
			pItem->setRowCount(0);
			delete pItem;
			pItem = nullptr;
		}
		m_pTreeModel->setRowCount(0);
	}
	
	//拿到巡检点数组
	QMap<QString, CPolPoint*> mapPolPoint =m_pPolAreaOld->m_mapPolPoint;
	QVector<CPolPoint*> vecPoint = mapPolPoint.values().toVector();
	qSort(vecPoint.begin(), vecPoint.end(), sortPoint);
	int nIndex = 0;
	for (int i=0;i<vecPoint.size(); i++, nIndex++)
	{
		QStandardItem *pItem = new QStandardItem(vecPoint[i]->m_sPointName);
		pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
		pItem->setData(vecPoint[i]->m_sPointGuid, 300);
		m_pTreeModel->setItem(nIndex, pItem);

		int k = 0;
		QMap<QString, CPolItem*> mapItem = m_pPolAreaOld->m_mapPolPoint[vecPoint[i]->m_sPointGuid]->m_mapPolItem;
		QVector<CPolItem*> vecItem = mapItem.values().toVector();
		qSort(vecItem.begin(), vecItem.end(), sortPolItem);
		for (int j = 0; j < vecItem.size(); j++)
		{
			CPolItem* pPolItem = vecItem[j];
			if (pPolItem->m_sPointGuid == vecPoint[i]->m_sPointGuid)
			{
				QStandardItem* pChildItem = new QStandardItem(pPolItem->m_sItemName);
				pChildItem->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
				pChildItem->setData(pPolItem->m_sItemGuid, 300);//将巡检项的Guid设置为300属性
				pChildItem->setData(pPolItem->m_nDevType, 301);//将巡检项的巡检类型设置为301属性
				pItem->setChild(k, pChildItem);
				k++;
			}
		}
	}
	//最后将树完全展开
	ui.treeView->expandAll();
}

void CRobWayPlanDialog::refushMap()
{
	QVector<QPointF>vecPoint;
	QVector<int>vecVal;
	//拿到巡检点数组
	QMap<QString, CPolPoint*> mapPolPoint =m_pPolAreaOld->m_mapPolPoint;
	QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
	qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);
	for (int i = 0; i < vecPolPoint.size(); i++)
	{
		CPolPoint* pPoint = vecPolPoint[i];
		double dPointLocation = pPoint->m_dPointLocation;//巡检点位置,从原点算，路径的总长度
		double dPointX = pPoint->m_dPointX;//巡检点X坐标（在地图中）
		double dPointY = pPoint->m_dPointY;//巡检点Y坐标（在地图中）
		double dPointZ = pPoint->m_dPointZ;//巡检点Z坐标（在地图中）
		vecPoint.push_back(QPointF(dPointX, dPointY));
		vecVal.push_back(dPointLocation);
	}
	ui.widgetMap->setPoints(vecPoint, vecVal);
	ui.widgetMap->setActPoints(m_pDevData->m_sRobCurrLoca.toFloat());
}

void CRobWayPlanDialog::slotRefushData()
{
	QString sStr = "";
	if (m_nIsMove == 1)//机器人运动中
	{
		float fSpeed = m_pDevData->m_sRobCurrSpeed.toFloat();             //当前速度
		sStr = QString::number(fSpeed) + "m/s";
	}
	else//显示设定速度
	{
		float fSpeed = m_pDevData->m_dRobRunSpeedSet.toFloat();             //设定速度
		sStr = QString::number(fSpeed) + "m/s";
	}
	ui.widgetControlButton->ui.pushButtonControl->SetPushButtenText(sStr);
	ui.widgetControlButton->ui.pushButtonControl->update();
	ui.widgetMap->setActPoints(m_pDevData->m_sRobCurrLoca.toFloat());
}

void CRobWayPlanDialog::slotExpand()
{
	ui.treeView->expandAll();
}

void CRobWayPlanDialog::slotShrink()
{
	ui.treeView->collapseAll();
}

void CRobWayPlanDialog::slotEditPoint(int nVal)
{
	QString sPointGuid = m_pPolAreaOld->getPointGuid(nVal);
	if (sPointGuid == "")
	{
		messageBox(0, A2T("请先在地图中选中需要编辑的点"));
		return;
	}
	QString sPointName = m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_sPointName;
	CRobPointConfigDialog oper(m_sGuid,sPointGuid,sPointName,m_pDev,m_pPolAreaOld,m_pBasicData, this);
	oper.exec();
	refushPointList();
}

void CRobWayPlanDialog::slotMovePoint(int nVal)
{
	QString sPointGuid = m_pPolAreaOld->getPointGuid(nVal);
	if (sPointGuid == "")
	{
		messageBox(0, A2T("请先在地图中选中需要编辑的点"));
		return;
	}
	CPolPoint* pPoint = m_pPolAreaOld->m_mapPolPoint[sPointGuid];
	CRobMovePointDlg oper(pPoint);
	oper.exec();
	refushMap();
	refushPointList();//刷新界面中的巡检点树
}

void CRobWayPlanDialog::slotDelPoint(int nVal)
{
	QString sPointGuid = m_pPolAreaOld->getPointGuid(nVal);
	//1.不存在的点不能删除
	if (sPointGuid == "")
	{
		messageBox(0, A2T("请先在地图中选中需要删除的点"));
		return;
	}
	//2.原点不能删除
	if (m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_dPointLocation == 0)
	{
		messageBox(0, A2T("不允许删除原点"));
		return;
	}
	delete m_pPolAreaOld->m_mapPolPoint[sPointGuid];
	m_pPolAreaOld->m_mapPolPoint.remove(sPointGuid);
	refushMap();
	refushPointList();//刷新界面中的巡检点树
}

void CRobWayPlanDialog::slotAddPoint(QPointF pCurrPoint, int nLocation)
{
	CPolPoint *pPolPointInfo = new CPolPoint();//new出新的巡检点
	pPolPointInfo->m_sGuid = m_sGuid;//设置场景Guid
	pPolPointInfo->m_sPolAreaGuid = m_pPolAreaOld->m_sPolAreaGuid;//设置区域Guid
	//设置 巡检点Guid
	QString sPointGuid = createGuid();
	pPolPointInfo->m_sPointGuid = sPointGuid;

	//用当前位置原始坐标
	pPolPointInfo->m_dPointX = pCurrPoint.x();//设置 巡检点坐标X
	pPolPointInfo->m_dPointY = pCurrPoint.y();//设置 巡检点坐标Y
	pPolPointInfo->m_dPointLocation = nLocation;//设置 巡检点位置X(PLC传入的位置)

	double dLocation = (double)nLocation / 1000;
	QString sStr = QString::number(dLocation, 'f', 1) + "m";
	pPolPointInfo->m_sPointName = sStr;//设置 巡检点名称
	pPolPointInfo->m_nIsChargePoint = 0;//设置 是否是充电点  1是  0不是
	pPolPointInfo->m_nPointStatus = 1;  //是新增的巡检点
	//根据传感器配置直接添加传感器巡检项
	for (int i = 0; i < m_pDevData->m_vecSensor.size(); i++)
	{
		SSensorInfo info = m_pDevData->m_vecSensor[i];
		int nSensorType = info.m_sSnesorType.toInt();

		if (info.m_bEnaled == false)//此类型的传感器不启用
		{
			continue;
		}
		else
		{
			CPolItemSensor* pItem = new CPolItemSensor();
			QString sItemGuid = createGuid();
			pItem->m_nItemStatus = 1;//新增的巡检项
			pItem->m_sItemGuid = sItemGuid;
			pItem->m_sPointGuid = sPointGuid;
			pItem->m_sGuid = m_sGuid;
			pItem->m_nAlarmLevel = 1;//默认报警等级1级
			pItem->m_nDevType = 0;
			if (nSensorType == enCO)
			{
				pItem->m_nIndex = enCO;
				pItem->m_sItemName = A2T("一氧化碳含量");
				pItem->m_nDevTypeCode = enCO;
				pItem->m_sDataThreadHoldHH = "42";//报警上上限
				pItem->m_sDataThreadHoldH = "32";
				pItem->m_sDataThreadHoldL = "-9999";
				pItem->m_sDataThreadHoldLL = "-9999";
			}
			else if (nSensorType == enGas)
			{
				pItem->m_nIndex = enGas;
				pItem->m_sItemName = A2T("可燃气体含量");
				pItem->m_nDevTypeCode = enGas;
				pItem->m_sDataThreadHoldHH = "35";//报警上上限
				pItem->m_sDataThreadHoldH = "25";
				pItem->m_sDataThreadHoldL = "-9999";
				pItem->m_sDataThreadHoldLL = "-9999";
			}
			else if (nSensorType == enPM25)
			{
				pItem->m_nIndex = enPM25;
				pItem->m_sItemName = A2T("PM2.5含量");
				pItem->m_nDevTypeCode = enPM25;
				pItem->m_sDataThreadHoldHH = "125";//报警上上限
				pItem->m_sDataThreadHoldH = "115";
				pItem->m_sDataThreadHoldL = "-9999";
				pItem->m_sDataThreadHoldLL = "-9999";
			}
			else if (nSensorType == enPM10)
			{
				pItem->m_nIndex = enPM10;
				pItem->m_sItemName = A2T("PM10含量");
				pItem->m_nDevTypeCode = enPM10;
				pItem->m_sDataThreadHoldHH = "230";//报警上上限
				pItem->m_sDataThreadHoldH = "250";
				pItem->m_sDataThreadHoldL = "-9999";
				pItem->m_sDataThreadHoldLL = "-9999";
			}
			else if (nSensorType == enHum)
			{
				pItem->m_nIndex = enHum;
				pItem->m_sItemName = A2T("环境湿度");
				pItem->m_nDevTypeCode = enHum;
				pItem->m_sDataThreadHoldHH = "80";//报警上上限
				pItem->m_sDataThreadHoldH = "70";
				pItem->m_sDataThreadHoldL = "-9999";
				pItem->m_sDataThreadHoldLL = "-9999";
			}
			else if (nSensorType == enUltrasonic)
			{
				pItem->m_nIndex = enUltrasonic;
				pItem->m_sItemName = A2T("超声波传感器巡检项");
				pItem->m_nDevTypeCode = enUltrasonic;
				pItem->m_sDataThreadHoldHH = "80";//报警上上限
				pItem->m_sDataThreadHoldH = "70";
				pItem->m_sDataThreadHoldL = "-9999";
				pItem->m_sDataThreadHoldLL = "-9999";
			}
			else if (nSensorType == enTemp)
			{
				pItem->m_nIndex = enTemp;
				pItem->m_sItemName = A2T("环境温度");
				pItem->m_nDevTypeCode = enTemp;
				pItem->m_sDataThreadHoldHH = "60";//报警上上限
				pItem->m_sDataThreadHoldH = "50";
				pItem->m_sDataThreadHoldL = "-5";
				pItem->m_sDataThreadHoldLL = "-15";
			}
			//将新建的传感器巡检项加入到新建的巡检点中
			pPolPointInfo->m_mapPolItem.insert(sItemGuid, pItem);
		}
	}
	//根据是否为原点直接默认添加三种云台持续检测起点巡检项
	if (nLocation == 0)
	{
		CPolItemCamer* pItemCamer = new CPolItemCamer();//图像相机
		QString sItemCamerGuid = createGuid();
		pItemCamer->m_sItemGuid = sItemCamerGuid;
		pItemCamer->m_nItemStatus = 1;//新增的巡检项
		pItemCamer->m_sItemName = A2T("图像相机持续监测起点");
		pItemCamer->m_sPointGuid = sPointGuid;
		pItemCamer->m_sGuid = m_sGuid;
		pItemCamer->m_nAlarmLevel = 1;//默认报警等级1级
		pItemCamer->m_nDevType = 1;//云台巡检项
		pItemCamer->m_nDevTypeCode = enCamer;//图像相机
		pItemCamer->m_nCheckType = 1;//持续任务主动上报
		pItemCamer->m_nCollectIndex = 0;//起点
		pItemCamer->m_nTackPicture = 1;//默认拍照
		pItemCamer->m_nDataType = 3;//默认是图像相机安全帽检测
		pItemCamer->m_nIndex = pPolPointInfo->getItemMaxIndex() + 1;
		pPolPointInfo->m_mapPolItem.insert(sItemCamerGuid, pItemCamer);

		CPolItemCamer* pItemIr= new CPolItemCamer();//红外相机
		QString sItemCamerIr = createGuid();
		pItemIr->m_sItemGuid = sItemCamerIr;
		pItemIr->m_nItemStatus = 1;//新增的巡检项
		pItemIr->m_sItemName = A2T("红外相机持续监测起点");
		pItemIr->m_sPointGuid = sPointGuid;
		pItemIr->m_sGuid = m_sGuid;
		pItemIr->m_nAlarmLevel = 1;//默认报警等级1级
		pItemIr->m_nDevType = 1;//云台巡检项
		pItemIr->m_nDevTypeCode = enIR;//红外相机
		pItemIr->m_nCheckType = 1;//持续任务主动上报
		pItemIr->m_nCollectIndex = 0;//起点
		pItemIr->m_nTackPicture = 1;//默认拍照
		pItemIr->m_nDataType = 6;//默认是温度巡检项-设备温度检测
		pItemIr->m_sDataThreadHoldHH = "90";//报警上上限
		pItemIr->m_sDataThreadHoldH = "80";
		pItemIr->m_sDataThreadHoldL = "-9999";
		pItemIr->m_sDataThreadHoldLL = "-9999";
		pItemIr->m_nIndex = pPolPointInfo->getItemMaxIndex() + 1;
		pPolPointInfo->m_mapPolItem.insert(sItemCamerIr, pItemIr);

		CPolItemCamer* pItemVioce = new CPolItemCamer();//声音传感器
		QString sItemCamerVioce = createGuid();
		pItemVioce->m_sItemGuid = sItemCamerVioce;
		pItemVioce->m_nItemStatus = 1;//新增的巡检项
		pItemVioce->m_sItemName = A2T("声音持续监测起点");
		pItemVioce->m_sPointGuid = sPointGuid;
		pItemVioce->m_sGuid = m_sGuid;
		pItemVioce->m_nAlarmLevel = 1;//默认报警等级1级
		pItemVioce->m_nDevType = 1;//云台巡检项
		pItemVioce->m_nDevTypeCode = enVoice;//声音传感器
		pItemVioce->m_nCheckType = 1;//持续任务主动上报
		pItemVioce->m_nCollectIndex = 0;//起点
		pItemVioce->m_nTackPicture = 1;//默认拍照
		pItemVioce->m_nDataType = 6;//默认是温度巡检项-设备温度检测
		pItemVioce->m_sDataThreadHoldHH = "95";//报警上上限
		pItemVioce->m_sDataThreadHoldH = "85";
		pItemVioce->m_sDataThreadHoldL = "-9999";
		pItemVioce->m_sDataThreadHoldLL = "-9999";
		pItemVioce->m_nIndex = pPolPointInfo->getItemMaxIndex() + 1;
		pPolPointInfo->m_mapPolItem.insert(sItemCamerVioce, pItemVioce);
	}
	//调用添加巡检点函数
	m_pPolAreaOld->m_mapPolPoint.insert(sPointGuid, pPolPointInfo);
	refushMap();
	refushPointList();//刷新界面中的巡检点树
}



void CRobWayPlanDialog::slotSelectedPoint(int nVal)
{
	QString sPointGuid = m_pPolAreaOld->getPointGuid(nVal);
	for (int row = 0; row < m_pTreeModel->rowCount(); ++row)
	{
		QModelIndex index = m_pTreeModel->index(row, 0); // 获取索引
		if (m_pTreeModel->data(index, 300).toString() == sPointGuid)
		{
			ui.treeView->scrollTo(index, QAbstractItemView::PositionAtCenter);
			// 获取树视图的视口
			QAbstractItemView *view = dynamic_cast<QAbstractItemView *>(ui.treeView);
			if (view)
			{
				// 获取树视图的视口矩形
				QRect rect = view->visualRect(index);

				// 计算单击事件的位置
				QPoint clickPos = rect.center();
				// 模拟单击事件
				QMouseEvent *clickEvent = new QMouseEvent(QEvent::MouseButtonPress, clickPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
				QApplication::sendEvent(view->viewport(), clickEvent);
				// 发送释放鼠标按钮事件
				clickEvent = new QMouseEvent(QEvent::MouseButtonRelease, clickPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
				QApplication::sendEvent(view->viewport(), clickEvent);
			}

		}
	}
}

void CRobWayPlanDialog::slotMoveMent(int nXVal)
{
	QString sPointGuid = m_pPolAreaOld->getPointGuid(nXVal);
	int nLoc = 0;
	//获取点的位置
	for (QMap<QString, CPolPoint*>::iterator itor = m_pPolAreaOld->m_mapPolPoint.begin(); itor != m_pPolAreaOld->m_mapPolPoint.end(); itor++)
	{
		CPolPoint* pPoint = itor.value();
		QString sGuid = itor.key();
		if (sPointGuid == sGuid)
		{
			nLoc = pPoint->m_dPointLocation;
			break;
		}
	}
	
	//控制函数
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_sGuid;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nLocaVal = nLoc;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveToPosition;
	m_pDev->ctrDev(stCtrInfo);
}

void CRobWayPlanDialog::slotCustomContextMenu(const QPoint &point)
{
	QModelIndex curIndex = ui.treeView->indexAt(point); // 取到当前的节点

	if (!curIndex.isValid())//当前的节点是无效的
	{
		return;
	}

	QStandardItem *item = m_pTreeModel->itemFromIndex(curIndex);
	if (!curIndex.parent().isValid())//当前节点的父节点无效，证明是1级节点->巡检点
	{
		QMenu menu(this);
		QAction aSet;
		QAction aSetChargeSta;
		QAction aSetChargeStaCancle;
		QAction aPaste;
		aSet.setText(A2T("编辑巡检点"));
		aSetChargeSta.setText(A2T("设置为充电点"));
		aSetChargeStaCancle.setText(A2T("取消充电点设定"));
		aPaste.setText(A2T("粘贴巡检项"));
		menu.addAction(&aSet);
		menu.addAction(&aSetChargeSta);
		menu.addAction(&aSetChargeStaCancle);
		menu.addAction(&aPaste);
		QAction* pAction = menu.exec(QCursor::pos());
		if (pAction && pAction->text() == A2T("编辑巡检点"))
		{
			QString sPointGuid = item->data(300).toString();
			QString sPointName = item->text();
			CRobPointConfigDialog oper(m_sGuid, sPointGuid, sPointName, m_pDev, m_pPolAreaOld,m_pBasicData,this);
			oper.exec();
			refushPointList();
		}
		if (pAction && pAction->text() == A2T("设置为充电点"))
		{
			QString sPointGuid = item->data(300).toString();
			//根据Guid找到对应的巡检点，并修改他的值
			setChargePoint(sPointGuid, 1);
		}
		if (pAction && pAction->text() == A2T("取消充电点设定"))
		{
			QString sPointGuid = item->data(300).toString();
			setChargePoint(sPointGuid, 0);
		}
		if (pAction && pAction->text() == A2T("粘贴巡检项"))
		{
			QString sPointGuid = item->data(300).toString();
			pasteItem(sPointGuid,m_sCopyPointGuid, m_sCopyItemGuid);
		}
	}
	else//当前的节点父节点有效，证明是二级节点-》巡检项
	{
		QMenu menu(this);
		QAction aDelete;//删除巡检项
		QAction aCopy;//复制巡检项
		aDelete.setText(A2T("删除巡检项"));
		aCopy.setText(A2T("复制巡检项"));
		menu.addAction(&aDelete);
		menu.addAction(&aCopy);
		QAction* pAction = menu.exec(QCursor::pos());
		if (pAction && pAction->text() == A2T("删除巡检项"))
		{
			QString sItemName = item->text();
			QString sItemGuid = item->data(300).toString();
			QStandardItem *itemParent = m_pTreeModel->itemFromIndex(curIndex.parent());
			QString sPointGuid = itemParent->data(300).toString();
			int nType = item->data(301).toInt();
			CPolItem* pItem =m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_mapPolItem[sItemGuid];
			m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_mapPolItem.remove(sItemGuid);
			//判断上层巡检点的状态
			if (m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_nPointStatus == 0)//未变
			{
				m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_nPointStatus = 2;
			}
			else if (m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_nPointStatus == 1)//新增
			{
				m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_nPointStatus = 1;
			}
			else if (m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_nPointStatus == 2)//修改过
			{
				m_pPolAreaOld->m_mapPolPoint[sPointGuid]->m_nPointStatus = 2;
			}
			delete pItem;
			pItem = nullptr;
			refushPointList();
		}
		else if (pAction && pAction->text() == A2T("复制巡检项"))
		{
			QString sItemGuid = item->data(300).toString();
			QStandardItem *itemParent = m_pTreeModel->itemFromIndex(curIndex.parent());
			QString sPointGuid = itemParent->data(300).toString();
			m_sCopyItemGuid = sItemGuid;
			m_sCopyPointGuid = sPointGuid;
		}
	}
}

void CRobWayPlanDialog::slotPushButtonEmergencyStopClicked()
{
	if (false == m_bIsStopButtonClicked)
	{
		//按下急停
		SCtrInfo stCtrInfo;
		stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
		stCtrInfo.m_bCtrSync = true;
		stCtrInfo.m_bHasCtr = false;
		stCtrInfo.m_nCtrCmd = ECCmdType::enCSuddenStop;
		stCtrInfo.m_nSpeed = m_nSpeed;
		m_pDev->ctrDev(stCtrInfo);
		CDevDataBase* pDevData = (CDevDataBase*)m_pDev->m_pDevDataBase;
		pDevData->m_bSuddenStop = true;
		//将本界面中的UI做出相应的变化
		setEmergencyStopDown();
		m_bIsStopButtonClicked = true;
	}
	else
	{
		//提醒一下是否确定松开急停
		int nRet = messageBox(3, A2T("是否确认松开急停?"));
		if (QMessageBox::No == nRet || QMessageBox::Close == nRet)
		{
			return;
		}
		SCtrInfo stCtrInfo;
		stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
		stCtrInfo.m_bCtrSync = true;
		stCtrInfo.m_bHasCtr = false;
		stCtrInfo.m_nCtrCmd = ECCmdType::enCNoCmd;
		stCtrInfo.m_nSpeed = m_nSpeed;
		m_pDev->ctrDev(stCtrInfo);
		CDevDataBase* pDevData = (CDevDataBase*)m_pDev->m_pDevDataBase;
		pDevData->m_bSuddenStop = false;
		//将本界面中的UI做出相应的变化
		setEmergencyStopUp();
		m_bIsStopButtonClicked = false;
	}
}

void CRobWayPlanDialog::slotPushButtonAddClicked()
{
	ui.widgetMap->addPoint();
}

void CRobWayPlanDialog::slotPushButtonChangeClicked()
{
	int nLocation = ui.widgetMap->m_nChoseXVal;
	slotEditPoint(nLocation);
}

void CRobWayPlanDialog::slotPushButtonDeleteClicked()
{
	int nLocation = ui.widgetMap->m_nChoseXVal;
	slotDelPoint(nLocation);
}

void CRobWayPlanDialog::slotPushButtonReChangeClicked()
{
	int nRet = messageBox(3, A2T("是否确认重新绘制"));
	if (QMessageBox::No == nRet || QMessageBox::Close == nRet)
	{
		return;
	}	
	m_pPolAreaOld->resetData();
	refushMap();
	refushPointList();//刷新界面中的巡检点树
}


void CRobWayPlanDialog::slotPushButtonSaveClicked()
{
	int nResult = messageBox(3, A2T("是否确认保存?"));
	if (QMessageBox::No == nResult || QMessageBox::Close == nResult)
	{
		return;
	}
	QString sReMsg = "";
	//保存成功
	if (upAreaCfg(m_pPolAreaOld, sReMsg))
	{
		m_pBasicData->m_pMutex->lock();
		m_pPolAreaOld->copyData(m_pBasicData->m_pPolArea);
		m_pBasicData->m_pMutex->unlock();
		messageBox(0, A2T("保存成功"));
	}
	else
	{
		messageBox(2, A2T("保存失败%1").arg(sReMsg));
	}
	
}

void CRobWayPlanDialog::slotPushButtonDeSpeedClicked()
{
	m_nSpeed--;
	if (m_nSpeed > 0)
	{
		if (m_nSpeed > 5)//速度限制
		{
			m_nSpeed = 5;
		}
	}
	else
	{
		m_nSpeed = 0;
	}
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCSetSpeed;
	stCtrInfo.m_nSpeed = m_nSpeed;
	m_pDev->ctrDev(stCtrInfo);
}

void CRobWayPlanDialog::slotPushButtonAddSpeedClicked()
{
	m_nSpeed++;//
	if (m_nSpeed > 0)
	{
		if (m_nSpeed > 5)//速度限制
		{
			m_nSpeed = 5;
		}
	}
	else
	{
		m_nSpeed = 0;
	}

	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCSetSpeed;
	stCtrInfo.m_nSpeed = m_nSpeed;
	m_pDev->ctrDev(stCtrInfo);
}

void CRobWayPlanDialog::slotPushButtonResetLocClicked()
{
	//触发是否复位
	int nResult = messageBox(3, A2T("是否确定进行复位?"));
	if (QMessageBox::No == nResult || QMessageBox::Close == nResult)
	{
		return;
	}
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCinitRob;
	m_pDev->ctrDev(stCtrInfo);
}

void CRobWayPlanDialog::slotPushButtonTalkBackFuncClicked()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enCVoiceTalk;
	bool bRet=m_pDev->ctrDev(stCtrInfo);
	if (bRet)
	{
		ui.widgetControlButton->ui.pushButtonTalkBackFunc->setIsTagVisible(true);
		ui.widgetControlButton->ui.pushButtonTalkBackClose->setIsTagVisible(false);
	}
}

void CRobWayPlanDialog::slotPushButtonTalkBackCloseClicked()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enCStopVoiceTalk;
	bool bRet=m_pDev->ctrDev(stCtrInfo);
	if (bRet)
	{
		ui.widgetControlButton->ui.pushButtonTalkBackFunc->setIsTagVisible(false);
		ui.widgetControlButton->ui.pushButtonTalkBackClose->setIsTagVisible(true);
	}
}

void CRobWayPlanDialog::slotPushButtonLampLightOpenClicked()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enCLightPwoer;
	bool bRet = m_pDev->ctrDev(stCtrInfo);
	if (bRet)
	{
		ui.widgetControlButton->ui.pushButtonLampLightOpen->setIsTagVisible(true);
		ui.widgetControlButton->ui.pushButtonLampLightClose->setIsTagVisible(false);
	}
}

void CRobWayPlanDialog::slotPushButtonLampLightCloseClicked()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enCStopLightPwoer;
	bool bRet = m_pDev->ctrDev(stCtrInfo);
	if (bRet)
	{
		ui.widgetControlButton->ui.pushButtonLampLightOpen->setIsTagVisible(false);
		ui.widgetControlButton->ui.pushButtonLampLightClose->setIsTagVisible(true);
	}
}
//调焦+按钮槽函数
void CRobWayPlanDialog::slotPushButtonFocusingAddClicked()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enZoomAdd;
	m_pDev->ctrDev(stCtrInfo);
}
//调焦+按钮释放槽函数
void CRobWayPlanDialog::slotPushButtonFocusingAddRelease()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enZoomAddStop;
	m_pDev->ctrDev(stCtrInfo);
}
//调焦-按钮槽函数
void CRobWayPlanDialog::slotPushButtonFocusingSubtractClicked()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enZoomSubtract;
	m_pDev->ctrDev(stCtrInfo);
}
//调焦-按钮释放槽函数
void CRobWayPlanDialog::slotPushButtonFocusingSubtractRelease()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDev->m_sDevGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	stCtrInfo.m_nCtrDevType = enSubDev1;
	stCtrInfo.m_nCtrCmd = ECCamerCmdType::enZoomSubtractStop;
	m_pDev->ctrDev(stCtrInfo);
}

void CRobWayPlanDialog::slotRobRunUp()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_sGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveUp;
	m_pDev->ctrDev(stCtrInfo);
}

void CRobWayPlanDialog::slotRobRunDown()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_sGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveDown;
	m_pDev->ctrDev(stCtrInfo);
}

void CRobWayPlanDialog::slotRobRunRight()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_sGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveForward;
	m_pDev->ctrDev(stCtrInfo);
	m_nIsMove = 1;
}

void CRobWayPlanDialog::slotRobRunLeft()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_sGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveBackward;
	m_pDev->ctrDev(stCtrInfo);
	m_nIsMove = 1;
}

void CRobWayPlanDialog::slotRobRunStop()
{
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_sGuid;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCNoCmd;
	m_pDev->ctrDev(stCtrInfo);
	m_nIsMove = 0;
}

void CRobWayPlanDialog::slotYtUpP()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCTiltUp;
	m_pDev->ctrDev(info);
}

void CRobWayPlanDialog::slotYtDownP()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCTiltDown;
	m_pDev->ctrDev(info);
}

void CRobWayPlanDialog::slotYtRigthP()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCPanRight;
	m_pDev->ctrDev(info);
}

void CRobWayPlanDialog::slotYtLeftP()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCPanLeft;
	m_pDev->ctrDev(info);
}

void CRobWayPlanDialog::slotYtUpR()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCTiltUpStop;
	m_pDev->ctrDev(info);
}

void CRobWayPlanDialog::slotYtDownR()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCTiltDownStop;
	m_pDev->ctrDev(info);
}

void CRobWayPlanDialog::slotYtRigthR()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCPanLeftStop;
	m_pDev->ctrDev(info);
}

void CRobWayPlanDialog::slotYtLeftR()
{
	SCtrInfo info;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCPanRightStop;
	m_pDev->ctrDev(info);
}

bool CRobWayPlanDialog::upAreaCfg(CPolArea * pPolArea, QString & sReInfo)
{
	QString sReMsg = "";
	QVector<QString>vecPointGuid;
	for (QMap<QString, CPolPoint*>::iterator itor = pPolArea->m_mapPolPoint.begin(); itor != pPolArea->m_mapPolPoint.end(); itor++)
	{
		vecPointGuid.push_back(itor.key());
	}
	//1.删除需要删除的巡检点
	if (!delPolPoint(m_sGuid,vecPointGuid, sReMsg))
	{
		sReInfo = A2T("数据库删除巡检点执行失败,") + sReMsg;
		return false;
	}
	//2.删除需要删除的巡检项
	if (!delPolItem(m_sGuid,vecPointGuid, sReMsg))
	{
		sReInfo = A2T("数据删除巡检项执行失败,") + sReMsg;
		return false;
	}
	for (QMap<QString, CPolPoint*>::iterator itor = pPolArea->m_mapPolPoint.begin(); itor != pPolArea->m_mapPolPoint.end(); itor++)
	{
		if (!upPointCfg(itor.value(), sReMsg))
		{
			sReInfo = A2T("更新巡检点信息执行失败,") + sReMsg;
			return false;
		}
	}
	return true;
}

bool CRobWayPlanDialog::upPointCfg(CPolPoint * pPolPoint, QString & sReInfo)
{
	QString sReMsg = "";
	if (pPolPoint->m_nPointStatus == 1) //增加巡检点和巡检点
	{
		if (!addPolPointInfo(pPolPoint, sReMsg))
		{
			sReInfo = A2T("添加巡检点失败，") + sReMsg;
			return false;
		}

		for (QMap<QString, CPolItem*>::iterator itor = pPolPoint->m_mapPolItem.begin(); itor != pPolPoint->m_mapPolItem.end(); itor++)
		{
			if (!addItemCfg(itor.value(), sReMsg))
			{
				sReInfo = A2T("添加巡检项失败，") + sReMsg;
				return false;
			}
			itor.value()->m_nItemStatus = 0;
		}
		pPolPoint->m_nPointStatus = 0;
	}
	//修改巡检点和巡检项
	else if (pPolPoint->m_nPointStatus == 2)
	{
		QVector<QString>vecItemId;
		for (QMap<QString, CPolItem*>::iterator itor = pPolPoint->m_mapPolItem.begin(); itor != pPolPoint->m_mapPolItem.end(); itor++)
		{
			vecItemId.push_back(itor.key());
		}
		//删除需要删除的巡检项
		if (!delPolItemEx(m_sGuid, pPolPoint->m_sPointGuid, vecItemId, sReMsg))
		{
			sReInfo = A2T("删除巡检项失败，") + sReMsg;
			return false;
		}
		for (QMap<QString, CPolItem*>::iterator itor = pPolPoint->m_mapPolItem.begin(); itor != pPolPoint->m_mapPolItem.end(); itor++)
		{
			//增加巡检项
			if (itor.value()->m_nItemStatus == 1)
			{
				if (!addItemCfg(itor.value(), sReMsg))
				{
					sReInfo = A2T("添加巡检项失败，") + sReMsg;
					return false;
				}
				itor.value()->m_nItemStatus = 0;
			}
			else if (itor.value()->m_nItemStatus == 2)
			{
				if (!upItemCfg(itor.value(), sReMsg))
				{
					sReInfo = A2T("修改巡检项失败，") + sReMsg;
					return false;
				}
				itor.value()->m_nItemStatus = 0;
			}
		}
		//保存巡检点名称
		if (!updatePointName(pPolPoint->m_sPointGuid, pPolPoint->m_sPointName, sReMsg))
		{
			sReInfo = A2T("修改巡检点名称失败，") + sReMsg;
			return false;
		}
		//保存巡检点位置
		if (!updatePointLocation(pPolPoint->m_sPointGuid, pPolPoint->m_dPointLocation, sReMsg))
		{
			sReInfo = A2T("修改巡检点位置失败，") + sReMsg;
			return false;
		}
		//保存是否充点电
		if (!updatePointCharge(pPolPoint->m_sPointGuid, pPolPoint->m_nIsChargePoint, sReMsg))
		{
			sReInfo = A2T("修改是否为充电电失败，") + sReMsg;
			return false;
		}
		pPolPoint->m_nPointStatus = 0;
	}
	return true;
}

bool CRobWayPlanDialog::addItemCfg(CPolItem * pPolItem, QString & sReInfo)
{
	if (pPolItem->m_nDevType == 1)
	{
		return addPloItemCarmer(*(CPolItemCamer *)pPolItem, sReInfo);
	}
	else
	{
		return addPloItemSensor(*(CPolItemSensor *)pPolItem, sReInfo);
	}
}

bool CRobWayPlanDialog::upItemCfg(CPolItem * pPolItem, QString & sReInfo)
{
	if (pPolItem->m_nDevType == 1)
	{
		return updatePloItemCamerByItemID(*(CPolItemCamer *)pPolItem, sReInfo);
	}
	else
	{
		return updatePloItemSensorByItemID(*(CPolItemSensor *)pPolItem, sReInfo);
	}
}
